<template>
    <div>
        <el-form
                :rules="rules"
                ref="form" :model="form" label-width="120px">
            <el-form-item label="所属上级类别" prop="parentId" disabled="false" v-if="form.parentName">
                <el-input v-model="form.parentName"
                          :disabled="true" placeholder="" clearable>
                </el-input>
            </el-form-item>
            
            
            <el-form-item label="分类名称" prop="name" disabled="false">
                <el-input v-model="form.name" :disabled="false" placeholder=""></el-input>
            </el-form-item>
            
            
            <el-form-item label="分类类型" prop="expendType" disabled="false">
                <el-select v-model="form.expendType" placeholder="请选择" clearable>
                    <el-option
                            v-for="item in expendTypeOptions"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            
            
            <el-form-item label="排序号" prop="orderNum" disabled="false">
                <el-input-number
                        :min="0" :step="1" controls-position="right"
                        v-model="form.orderNum" :disabled="false" placeholder=""></el-input-number>
            </el-form-item>
            
            <el-form-item label="备注" prop="remarks" disabled="false">
                <el-input
                        type="textarea"
                        v-model="form.remarks" :disabled="false" placeholder=""></el-input>
            </el-form-item>
        
        
        </el-form>
    </div>
</template>
<script>
  import { findComponentUpward } from '@/utils/findVueDom';
  
  import * as api from '@/apis/sysclass.api.js';
  import { deepClone } from '@/utils/index';
  import { ajaxDicList } from '@/utils/dic';
  import { fillData, validatePromise } from '../../../utils';
  
  
  const INIT_FORM_DATA = {
    id: '',
    name: '',
    code: '',
    state: '1',
    classId: '',
    parentName: '',
    website: '',
    logo: '',
    description: '',
    dataSourcesType: '',
    dataSourcesUser: '',
    dataSources: '',
    tenantType: '',
    tenantId: '',
    delState: '',
    addUser: '',
    addUserId: '',
    addTime: '',
    editUser: '',
    editUserId: '',
    lastEditTime: '',
    serverNumber: '',
    parentId: '0',//默认添加的是根目录 0
    typeInfo: '',// from parent
    expendType: '',
  };
  export default {
    data() {
      return {
        api: api,
        title: '管理',
        dialogVisible: false,
        form: deepClone(INIT_FORM_DATA),
        rules: {
          
          id: [{ required: false, trigger: 'blur', message: '主键ID不能为空' }],
          
          name: [{ required: true, trigger: 'blur', message: '名称不能为空' }],
          
          code: [{ required: true, trigger: 'blur', message: '标识码不能为空' }],
          
          state: [{ required: false, trigger: 'blur', message: '状态 1 使用中  5停用不能为空' }],
          
          website: [
            { required: true, trigger: 'blur', message: '描述不能为空' },
            { type: 'url', trigger: 'blur', message: '请输入url格式  eg:https://www.szqerp.com ' },
          ],
          
          logo: [{ required: false, trigger: 'blur', message: '描述不能为空' }],
          
          description: [{ required: false, trigger: 'blur', message: '描述不能为空' }],
          
          dataSourcesType: [{ required: false, trigger: 'blur', message: '数据来源类型 1、系统产生  2、外部导出不能为空' }],
          
          dataSourcesUser: [{ required: false, trigger: 'blur', message: '数据来源操作者(当为导入数据，记录 导入人)不能为空' }],
          
          dataSources: [{ required: false, trigger: 'blur', message: '数据来源（当为导入数据时记录导入流水号）不能为空' }],
          
          tenantType: [{ required: false, trigger: 'blur', message: '租户类型 1应用管理者2应用消息者不能为空' }],
          
          tenantId: [{ required: false, trigger: 'blur', message: '租户ID不能为空' }],
          
          delState: [{ required: false, trigger: 'blur', message: '有效状态[数据字典=delState] 1有效  5删除不能为空' }],
          
          addUser: [{ required: false, trigger: 'blur', message: '添加人不能为空' }],
          
          addUserId: [{ required: false, trigger: 'blur', message: '添加人ID不能为空' }],
          
          addTime: [{ required: false, trigger: 'blur', message: '添加时间不能为空' }],
          
          editUser: [{ required: false, trigger: 'blur', message: '最近更新人不能为空' }],
          
          editUserId: [{ required: false, trigger: 'blur', message: '最近更新人ID不能为空' }],
          
          lastEditTime: [{ required: false, trigger: 'blur', message: '最近更新时间不能为空' }],
          
          serverNumber: [{ required: false, trigger: 'blur', message: '服务器编号ID不能为空' }],
          
          expendType: [{ required: false, trigger: 'blur', message: '必填' }],
          
        },
        expendTypeOptions: [],
      };
    },
    methods: {
      handleClose() {
        this.$refs['form'].resetFields();
        this.$data.dialogVisible = false;
      },
      /**
       * @description:
       * @params _data.id
       * @params _data.typeInfo
       * @return
       */
      initByDialog(_data) {
        fillData(this.form, _data);
        
        //设置默认
        this.getExtendInfoOption();
        
        
      },
      /**
       * @description: 添加 下拉数据
       * @params
       * @return
       */
      async getExtendInfoOption() {
        var expendTypeOptions = await ajaxDicList('sys_class_typeInfo');
        this.expendTypeOptions = expendTypeOptions || [];
      },
      
      
      setParent(_parent) {
        if (_parent) {
          this.form.parentId = _parent.id;
        }
        
      },
      setTypeInfo() {
        var classDialog = findComponentUpward(this, 'classDialog');
        var typeInfo = classDialog && classDialog.$data.$_options && classDialog.$data.$_options.typeInfo;
        this.form.typeInfo = typeInfo;
      },
      setExpendType() {
        var classDialog = findComponentUpward(this, 'classDialog');
        var expendType = classDialog && classDialog.$data.$_options && classDialog.$data.$_options.expendType;
        this.form.expendType = expendType;
        debugger
      },
      async submitByDialog() {
        var isyes = await validatePromise.call(this, 'form');
        if (!isyes) {
          return { success: false };
        }
        var _params = deepClone(this.form);
        console.log('[get]  /erpPlatform/add');
        console.log('参数', JSON.stringify(_params));
        var ajaxFun = this.form.id ? this.api.update : this.api.add;
        var result = await ajaxFun(_params).then(res => res.data);
        console.log('结果', result);
        this.dialogVisible = false;
        this.$emit('refresh', { parentId: this.form.parentId });
        return { success: true, data: this.form };
      },
    },
  };
</script>
